import json

import requests
from utils.configurator import config
from utils.logger import logger
from time import sleep
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from utils.utils import Utils


class BaseApi:
    """封装requests库："""
    def __init__(self):
        self.base_url = config["api_url"]
        self.proxies = {
            # "http": config["proxy"],
            # "https": config["proxy"],
            "http": config.get("proxy"),
            "https": config.get("proxy"),
        }
        self.session = requests.Session()
        self.session.verify = False
        self.json_data = None
        # self.status_code = None

    def send_request(self, *args, **kwargs):
        # logger.debug(f"Request: \n{args}, \n{kwargs}")
        logger.debug(f"Request: \n{args}, \n{kwargs}")
        # disable InsecureRequestWarning
        sleep(2)
        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
        if self.proxies:
            response = self.session.request(*args, **kwargs, proxies=self.proxies)
        response = self.session.request(*args, **kwargs)
        logger.debug(f"Response: \n{response.text}")
        # logger.debug(f"Response: " + json.dumps(response.text, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        # self.status_code = response.status_code
        return response

    def jsonpath(self, expr):
        return Utils.jsonpath(self.json_data, expr)


if __name__ == '__main__':
    ba = BaseApi()
    ba.json_data = {'code': 200, 'msg': 'ok', 'data': [{'id': 1, 'name': 'aaa'}, {'id': 2, 'name': 'bbb'}, {'id': 3, 'name': 'ccc'}] }
    # import json
    # ba.json_data = json.dumps(dict_data)
    print(ba.json_data)
    ids = ba.jsonpath("$..id")
    print(ids)